package com.ptmall.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.ptmall.app.MainActivity;
import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpFragment;
import com.ptmall.app.bean.MessageBean;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.net.HttpMethods;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.api.ApiService;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.WebActivity;
import com.ptmall.app.ui.adapter.MsgListAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PtMsgFragment extends BaseMvpFragment {


    PullableListView mListview;
    PullToRefreshLayout mRefreshView;
    View view;
    int page = 1;
    List<MessageBean> mDataLists = new ArrayList<>();

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    MsgListAdapter mMsgListAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_list,container,false);
        return view;

    }


    @Override
    public void initViews() {
        mRefreshView = (PullToRefreshLayout) view.findViewById(R.id.refresh_view);
        mListview = (PullableListView) view.findViewById(R.id.listview);
        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getlist();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getlist();
            }
        });
    }

    @Override
    public void initEvents() {
        mMsgListAdapter = new MsgListAdapter(getContext(),null);
        mListview.setAdapter(mMsgListAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        getlist();

    }

    private void getlist(){
        apiDataRepository.getMsgList(null, new ApiNetResponse<PageData<MessageBean>>() {
            @Override
            public void onSuccess(PageData<MessageBean> data) {
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (page == 1) {
                    mDataLists.clear();
                    if (null != data.getData() && data.getData().size() > 0) {
                        mDataLists.addAll(data.getData());
                    } else {
                    }
                } else {
                    if (null != data.getData() && data.getData().size() > 0) {
                        mDataLists.addAll(data.getData());
                    } else {
                        showMsg(R.string.has_no_more);
                    }
                }
                mMsgListAdapter.pushData(data.getData());
                mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

//                        Map<String,String> map = new HashMap<>();
//                        map.put("noticeId",data.getData().get(i).noticeId);
                        Intent intent = new Intent(getContext(), WebActivity.class);
                        intent.putExtra("title","消息");
                        intent.putExtra("url", HttpMethods.BASE_URL+ ApiService.MESSAGEURL+"/noticeId/"+data.getData().get(i).noticeId+"/memberId/"+ MainActivity.MEMBERID);
                        startActivity(intent);
//                        apiDataRepository.getMsgUrl(map, new ApiNetResponse<Object>(getContext()) {
//                            @Override
//                            public void onSuccess(Object data) {
//
//                            }
//                        });
                    }
                });
            }
        });
    }
}
